package com.ihighest.app.common.base;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.ihighest.app.common.core.Application;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

/**
 * Activity 父类
 * 
 * @author Vity
 * @date 2015-5-11
 * @mail vity.1215@gmail.com
 */
public abstract class AbstractActivity extends Activity {

	/** 资源对象 */
	public Resources RES;

	/** 是否退出 */
	boolean isExit = false;
	private long exitTime = 0;// 点击退出键的时间

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/* 定义资源对象 */
		RES = getResources();
		/* 优化输入法 */
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

		/* 友盟push 统计应用启动数据 */
		PushAgent.getInstance(this).onAppStart();

	}

	@Override
	public void onResume() {
		super.onResume();
		// 友盟移动统计
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		// 友盟移动统计
		if (((Application) getApplication()).mLocationClient != null) {
			((Application) getApplication()).mLocationClient.stop();
		}
		MobclickAgent.onPause(this);
	}

	// @Override
	// public boolean onKeyDown(int keyCode, KeyEvent event) {
	// // 当keyCode等于退出事件值时
	// if (keyCode == KeyEvent.KEYCODE_BACK) {
	// exit();
	// return false;
	// } else {
	// return super.onKeyDown(keyCode, event);
	// }
	// }

	public void exit() {
		if ((System.currentTimeMillis() - exitTime) > 2000) {
			Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
			exitTime = System.currentTimeMillis();
		} else {
			finish();
			Application.getInstance().exit();
			System.exit(0);
		}
	}

	@Override
	public void onBackPressed() {
		exit();

	}

	static class MsgHandler extends Handler {
		private WeakReference<AbstractActivity> mActivity;

		MsgHandler(AbstractActivity activity) {
			mActivity = new WeakReference<AbstractActivity>(activity);
		}

		@Override
		public void handleMessage(Message msg) {
			AbstractActivity activity = mActivity.get();
			activity.isExit = false;
		}
	}

	public Handler rootHandler = new MsgHandler(this);

	/**
	 * 从当前activity跳转到目标activity,<br>
	 * 如果目标activity曾经打开过,就重新展现,<br>
	 * 如果从来没打开过,就新建一个打开
	 * 
	 * @param cls
	 */
	public void gotoExistActivity(Class<?> cls) {
		Intent intent;
		intent = new Intent(this, cls);
		intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(intent);
	}

	/**
	 * 从当前activity跳转到目标activity,<br>
	 * 如果目标activity曾经打开过,就重新展现,<br>
	 * 如果从来没打开过,就新建一个打开
	 * 
	 * @param cls
	 */
	public void gotoExistActivity(Class<?> cls, Bundle bundle) {
		Intent intent;
		intent = new Intent(this, cls);
		intent.putExtras(bundle);
		intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(intent);
	}

	/**
	 * 新建一个activity打开
	 * 
	 * @param cls
	 */
	public void gotoActivity(Class<?> cls) {
		Intent intent;
		intent = new Intent(this, cls);
		startActivity(intent);
	}

	/**
	 * 新建一个activity打开
	 * 
	 * @param cls
	 */
	public void gotoActivity(Class<?> cls, Bundle bundle) {
		Intent intent;
		intent = new Intent(this, cls);
		intent.putExtras(bundle);
		startActivity(intent);
	}

	/**
	 * 获取EditText输入的值
	 * 
	 * @param editText
	 * @return
	 */
	public String getEditTextValue(EditText editText) {
		return editText.getText().toString();
	}

}
